package kp.rpc.spring.boot.stater.bootStrap;

import kp.rpc.RpcApplication;
import kp.rpc.server.tcp.VertxTcpServer;
import kp.rpc.spring.boot.stater.annotation.EnableKpRpc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Map;


@Slf4j
public class RpcInitBootStrap implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //获取EnableRpc的注解上的值
        Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(EnableKpRpc.class.getName());
        boolean enable = false;
        assert annotationAttributes != null;
        if (!annotationAttributes.isEmpty()) {
            enable = (boolean) annotationAttributes.get("open");
        }
        //初始化
        RpcApplication.init();
        if (enable) {
            //初始化
            VertxTcpServer vertxTcpServer = new VertxTcpServer();
            vertxTcpServer.doStart(RpcApplication.getRpcConfig().getServerPort());
            log.info("kp-rpc init success");
        }
    }
}
